﻿#region File Header
//-----------------------------------------------------------------------------
// SibLib
//
// Copyright (C) 2010 Julien Villers
// This program is distributed under the terms of the 
// GNU Lesser General Public License (LGPL).
// See Docs/lgpl.txt and Docs/gpl.txt
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Globalization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SibLib
{
    public static class Conversions
    {
        #region Vectors & Points <=> String
        
        private static char[] SEPARATORS = { ';' };

        public static string Vector2ToString(Vector2 vector)
        {
            return vector.X.ToString(CultureInfo.InvariantCulture) + ';' + vector.Y.ToString(CultureInfo.InvariantCulture);
        }

        public static Vector2 StringToVector2(string str)
        {
            Vector2 ret = new Vector2();
            string[] items = str.Split(SEPARATORS);
            ret.X = float.Parse(items[0], CultureInfo.InvariantCulture);
            ret.Y = float.Parse(items[1], CultureInfo.InvariantCulture);
            return ret;
        }

        public static string PointToString(Point point)
        {
            return point.X.ToString(CultureInfo.InvariantCulture) + ';' + point.Y.ToString(CultureInfo.InvariantCulture);
        }

        public static Point StringToPoint(string str)
        {
            Point ret = new Point();
            string[] items = str.Split(SEPARATORS);
            ret.X = int.Parse(items[0], CultureInfo.InvariantCulture);
            ret.Y = int.Parse(items[1], CultureInfo.InvariantCulture);
            return ret;
        }

        public static string Vector3ToString(Vector3 vector)
        {
            return vector.X.ToString(CultureInfo.InvariantCulture) + ';' + vector.Y.ToString(CultureInfo.InvariantCulture) + ';' + vector.Z.ToString(CultureInfo.InvariantCulture);
        }

        public static Vector3 StringToVector3(string str)
        {
            Vector3 ret = new Vector3();
            string[] items = str.Split(SEPARATORS);
            ret.X = float.Parse(items[0], CultureInfo.InvariantCulture);
            ret.Y = float.Parse(items[1], CultureInfo.InvariantCulture);
            ret.Z = float.Parse(items[2], CultureInfo.InvariantCulture);
            return ret;
        }

        #endregion

        #region Colors

        // TODO: make this an extension method
        public static Color UintToColor(uint value)
        {
            Color col = new Color();
            col.PackedValue = value;
            return col;
        }

        #endregion
    }
}
